#include "Stdafx.h"
#include "DirectInput8.h"

using namespace Vortex::Wrapper::DInput;

DirectInput8::DirectInput8() {
	IDirectInput8W* pDirectInput = 0;

	// create the DirectInput interface
	HRESULT hr = DirectInput8Create( GetModuleHandle(NULL),
		DIRECTINPUT_VERSION,
		IID_IDirectInput8,
		(void**)&pDirectInput,
		NULL
	);

	if (hr) {
		throw gcnew Exception("Can't create DirectInput8 interface");
	}

	_handle = pDirectInput;
}

DirectInput8::~DirectInput8() {
	_handle->Release();
}

///<summary>Creates input device of the specified type</summary>
HResult DirectInput8::CreateKeyboard([Out] DirectInputKeyboard8^% device) {
	IDirectInputDevice8W* pDirectInputDevice = NULL;
	device = nullptr;

	// create the keyboard device
	HResult result = HResult(
		_handle->CreateDevice(GUID_SysKeyboard,
		&pDirectInputDevice,
		NULL)
	);	

	if (result.IsSuccess) {
		device = gcnew DirectInputKeyboard8(pDirectInputDevice);
	}

	return result;
}